package com.ge.modules.api.secondController;

import com.ge.common.annotation.LoginStoreApi;
import com.ge.common.utils.DateUtils;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbStoreDto;
import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.api.service.TbStoreService;
import com.ge.modules.api.service.TbTechnicianService;
import com.ge.modules.entity.TbOrderEntity;
import com.ge.modules.entity.TbStoreEntity;
import com.ge.modules.entity.TbTechnicianEntity;
import com.ge.modules.entity.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 店铺
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-20 17:02:44
 */
@RestController
@RequestMapping("/api/store")
public class TbStoreController {

    @Autowired
    private TbStoreService storeService;

    @Autowired
    private TbTechnicianService tbTechnicianService;

    @Autowired
    private TbOrderService tbOrderService;

    /**
     * 查询我的店铺的技师
     *
     * @param tbStoreEntity
     * @param params
     * @return
     */
    @GetMapping("/technicianList")
    public R technicianList(@LoginStoreApi TbStoreEntity tbStoreEntity, @RequestParam Map<String, Object> params) {
        params.put("storeId", tbStoreEntity.getStoreId());
        //查询列表数据
        Query query = new Query(params);
        List<TbTechnicianEntity> tbCommodityEntityList = tbTechnicianService.queryList(query);
        int total = tbTechnicianService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbCommodityEntityList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 店铺保存or修改技师信息 实则修改技师所属店铺
     *
     * @param tbStoreEntity
     * @param tbTechnicianEntity
     * @return
     */
    @PostMapping("/saveTechnician")
    public R saveTechnician(@LoginStoreApi TbStoreEntity tbStoreEntity, TbTechnicianEntity tbTechnicianEntity) {
        TbTechnicianEntity technician = null;
        if (tbTechnicianEntity.getTechnicianId() == null) {
            TbTechnicianEntity technicianEntity = tbTechnicianService.queryByMobile(tbTechnicianEntity.getStoreMobile());
            //根据电话号码查询
            if (technicianEntity == null) {
                return R.error("技师不存在");
            }
            technician = technicianEntity;
        } else {
            technician = tbTechnicianService.queryByTechnicianId(tbTechnicianEntity.getTechnicianId());
            //根据id查询
            if (technician == null) {
                return R.error("技师不存在");
            }
        }
        technician.setNickname(tbTechnicianEntity.getNickname());
        technician.setGrade(tbTechnicianEntity.getGrade());
        technician.setStoreId(tbStoreEntity.getStoreId());
        technician.setStoreMobile(tbTechnicianEntity.getStoreMobile());
        technician.setHeadpicture(tbTechnicianEntity.getHeadpicture());
        technician.setSignature(tbTechnicianEntity.getSignature());
        technician.setBirthday(tbTechnicianEntity.getBirthday());
        technician.setSex(tbTechnicianEntity.getSex());
        technician.setWorkAgeLimit(tbTechnicianEntity.getWorkAgeLimit());
        technician.setTechnicianType(tbTechnicianEntity.getTechnicianType());
        //用户角度来说 输入的数字除以100 方便计算
        technician.setCommissionRate(tbTechnicianEntity.getCommissionRate());
        tbTechnicianService.update(technician);
        return R.ok("设置成功");
    }

    /**
     * 删除店铺技师 即清空技师的店铺id
     *
     * @param tbStoreEntity
     * @param tbTechnicianEntity
     * @return
     */
    @PostMapping("/updateTechnician")
    public R updateTechnician(@LoginStoreApi TbStoreEntity tbStoreEntity, TbTechnicianEntity tbTechnicianEntity) {
        TbTechnicianEntity technician = tbTechnicianService.queryByTechnicianId(tbTechnicianEntity.getTechnicianId());
        technician.setStoreId(0);
        tbTechnicianService.update(technician);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{technicianId}")
    public R info(@PathVariable("technicianId") Integer technicianId) {
        TbTechnicianEntity technician = tbTechnicianService.queryByTechnicianId(technicianId);
        return R.ok().put("technician", technician);
    }


    /**
     * 设置上下班时间
     *
     * @param tbStoreEntity
     * @param storeEntity
     * @return
     */
    @GetMapping("/setStartTimeAndEndTime")
    public R setStartTimeAndEndTime(@LoginStoreApi TbStoreEntity tbStoreEntity, TbStoreEntity storeEntity) {
        tbStoreEntity.setStartWorkTime(storeEntity.getStartWorkTime());
        tbStoreEntity.setEndWorkTime(storeEntity.getEndWorkTime());
        storeService.update(tbStoreEntity);
        return R.ok();
    }

    /**
     * 查询预约列表
     *
     * @param tbStoreEntity
     * @param params
     * @return
     */
    @GetMapping("/getAppoint")
    public R getAppoint(@LoginStoreApi TbStoreEntity tbStoreEntity, @RequestParam Map<String, Object> params) {
//        Map<String, Object> params = new HashMap<>();
        params.put("commodityMasterStoreId", tbStoreEntity.getStoreId());
        String dates = (String) params.get("dates");
        //在拼接一天的开始时间和结束时间
        String startDates = dates + " 00:00:00";
        String endDates = dates + " 23:59:59";
        params.put("startTime", DateUtils.toDate(startDates, DateUtils.DATE_TIME_PATTERN));
        params.put("endTime", DateUtils.toDate(endDates, DateUtils.DATE_TIME_PATTERN));
        //查询列表数据
        Query query = new Query(params);
        List<TbOrderEntity> tbCommodityEntityList = tbOrderService.queryList(query);
        int total = tbOrderService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbCommodityEntityList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查询店铺的会员
     *
     * @param tbStoreEntity
     * @return
     */
    @GetMapping("/getMember")
    public R getMember(@LoginStoreApi TbStoreEntity tbStoreEntity) {
        List<TbUser> tbUserList = storeService.queryStoreUser(tbStoreEntity.getStoreId());
        return R.ok().put("date", tbUserList);
    }

    /**
     * 修改店铺信息
     *
     * @param tbStoreEntity
     * @param storeEntity
     * @return
     */
    @PostMapping("/updateStore")
    public R updateStore(@LoginStoreApi TbStoreEntity tbStoreEntity, TbStoreEntity storeEntity) {
        storeEntity.setStoreId(tbStoreEntity.getStoreId());
        storeService.update(storeEntity);
        return R.ok();
    }

    /**
     * 查询店铺信息 统计总订单 总业绩 总客户
     *
     * @param tbStoreEntity
     * @return
     */
    @PostMapping("/storeInfo")
    public R storeInfo(@LoginStoreApi TbStoreEntity tbStoreEntity) {
        //统计总订单 查询所有订单
        Map<String, Object> map = new HashMap<>();
        map.put("commodityMasterStoreId", tbStoreEntity.getStoreId());
        int totalOne = tbOrderService.queryTotal(map);
        //统计总业绩 查询订单状态为5 已完成的订单
        map.put("orderStatus", 5);
        int totalTwo = tbOrderService.queryTotal(map);
        //统计总客户
        Map<String, Object> map1 = new HashMap<>();
        map1.put("commodityMasterStoreId", tbStoreEntity.getStoreId());
        int totalUsers = tbOrderService.countStoreUsers(map1);
        TbStoreDto tbStoreDto = tbStoreEntity.toDto();
        tbStoreDto.setTotalCompleteOrders(totalTwo);
        tbStoreDto.setTotalOrders(totalOne);
        tbStoreDto.setTotalUsers(totalUsers);
        return R.ok().put("data", tbStoreDto);
    }
}
